<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions"><template #toolbar_btns></template></VTable>
  </VPage>
</template>
  
<script setup lang="jsx" name="Account">
import api from './api'

const tableRef = ref()
// 查询
const query = () => {
  tableRef?.value.query()
}

const router = useRouter()
const handleRow = (name, row, rowIndex) => {
  // const showLoading = () => {
  //   loadings[name + rowIndex] = true
  // }
  // const closeLoading = () => {
  //   loadings[name + rowIndex] = false
  // }
  const setTableForm = (tableForm) => sessionStorage.setItem('_table_form', JSON.stringify({ tableForm }))
  const handle = {
    1: () => {
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] } })
      router.push('/manage/order')
    },
    2: () => {
      setTableForm({ purchase_user_id: [row.purchase_user_id], change_status: [6] })
      router.push('/demand/change')
    },
    3: () => {
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, audit_status: [1] })
      router.push('/manage/process')
    },
    4: () => {
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, is_platform_order: [1], purchase_type: [1] })
      router.push('/manage/process')
    },
    5: () => {
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, purchase_label: [2] })
      router.push('/manage/process')
    },
    6: () => {
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, audit_status: [2] })
      router.push('/manage/audit')
    },
    7: () => {
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, finance_status: [1] })
      router.push('/manage/purchase')
    },
    8: () => {
      setTableForm({ purchase_type: [1], payment_status: [1,2,3,4,5] })
      router.push('/finance/payment')
    },
    9: () => { // 阿里账期已入库未申请付款-跳转到采购单管理页面,按采购人员统计采购单为阿里账期的，且入库状态为已入库的，财务状态为待申请付款的采购单
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, payment_type: [4], purchase_status: [ 4 ], finance_status: [ 1 ] })
      router.push('/manage/purchase')
    },
    10: () => { // 部分入库-跳转到采购单管理页面,按采购人员统计采购状态为部分入库的采购单
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, purchase_status: [ 3 ] })
      router.push('/manage/purchase')
    },
    11: () => { // 已入库-跳转到采购单管理页面,按采购人员统计采购状态为已入库的采购单
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, purchase_status: [ 4 ] })
      router.push('/manage/purchase')
    },
    12: () => { // 部分付款-跳转到采购单管理页面,按采购人员统计采购单财务状态为部分付款
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, finance_status: [ 3 ] })
      router.push('/manage/purchase')
    },
    13: () => { // 收货异常待处理-跳转到异常单管理页面，按采购人员统计异常单待采购处理的异常单
      setTableForm({ purchase_user_id: [row.purchase_user_id], abnormal_status: [ 1 ] })
      router.push('/manage/abnormal')
    },
    14: () => { // 待冲账-跳转到采购单管理页面，按采购人员统计采购单财务状态为待冲账
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, finance_status: [ 4 ] })
      router.push('/manage/purchase')
    },
    15: () => { // 冲账完成-跳转到采购单管理页面，按采购人员统计采购单财务状态为冲账完成
      setTableForm({ user: { type: 'purchase_user_id', value: [row.purchase_user_id] }, finance_status: [ 6 ] })
      router.push('/manage/purchase')
    },
  }
  handle[name]()
}

// 列表配置
const tableOptions = reactive({
  showHeaderOverflow: false,
  showOverflow: false,
  columns: [
    { title: '采购员', field: 'purchase_user_name', width: 140, line: 2 },
    {
      title: '备库需求待下单（sku）', field: 'demand_wait_order_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.demand_wait_order_num} type={row.demand_wait_order_num ? 'link' : undefined} onClick={() => handleRow(1, row)} />
      }
    },
    {
      title: '需求变更待处理', field: 'change_process_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.change_process_num} type={row.change_process_num ? 'link' : undefined} onClick={() => handleRow(2, row)} />
      }
    },
    {
      title: '采购单待提交', field: 'wait_submit_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.wait_submit_num} type={row.wait_submit_num ? 'link' : undefined} onClick={() => handleRow(3, row)} />
      }
    },
    {
      title: '1688待下单', field: 'alibaba_wait_order_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.alibaba_wait_order_num} type={row.alibaba_wait_order_num ? 'link' : undefined} onClick={() => handleRow(4, row)} />
      }
    },
    {
      title: '1688下单失败', field: 'alibaba_order_fail_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.alibaba_order_fail_num} type={row.alibaba_order_fail_num ? 'link' : undefined} onClick={() => handleRow(5, row)} />
      }
    },
    { title: '采购单待审核', field: 'wait_audit_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.wait_audit_num} type={row.wait_audit_num ? 'link' : undefined} onClick={() => handleRow(6, row)} />
      }
    },
    { title: '待申请付款', field: 'wait_apply_payment_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.wait_apply_payment_num} type={row.wait_apply_payment_num ? 'link' : undefined} onClick={() => handleRow(7, row)} />
      }
    },
    { title: '1688已申请未付款', field: 'alibaba_apply_unpaid_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.alibaba_apply_unpaid_num} type={row.alibaba_apply_unpaid_num ? 'link' : undefined} onClick={() => handleRow(8, row)} />
      }
    },
    { title: '阿里账期已入库未申请付款', field: 'period_inbound_unpaid_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.period_inbound_unpaid_num} type={row.period_inbound_unpaid_num ? 'link' : undefined} onClick={() => handleRow(9, row)} />
      }
    },
    { title: '部分入库', field: 'part_inbound_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.part_inbound_num} type={row.part_inbound_num ? 'link' : undefined} onClick={() => handleRow(10, row)} />
      }
    },
    { title: '已入库', field: 'all_inbound_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.all_inbound_num} type={row.all_inbound_num ? 'link' : undefined} onClick={() => handleRow(11, row)} />
      }
    },
    { title: '部分付款', field: 'part_payment_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.part_payment_num} type={row.part_payment_num ? 'link' : undefined} onClick={() => handleRow(12, row)} />
      }
    },
    { title: '收货异常待处理', field: 'abnormal_wait_process_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.abnormal_wait_process_num} type={row.abnormal_wait_process_num ? 'link' : undefined} onClick={() => handleRow(13, row)} />
      }
    },
    { title: '待冲账', field: 'wait_strike_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.wait_strike_num} type={row.wait_strike_num ? 'link' : undefined} onClick={() => handleRow(14, row)} />
      }
    },
    { title: '冲账完成', field: 'all_strike_num', width: 80, slots: {
        default: ({ row }) => <VText value={row.all_strike_num} type={row.all_strike_num ? 'link' : undefined} onClick={() => handleRow(15, row)} />
      }
    },
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page })
    }
  }
})
</script>
  